package com.sw.haruka.module.share.monitor;

import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sw.haruka.R;
import com.sw.haruka.helper.utils.FileUtils;
import com.sw.haruka.model.FileRequestManager;
import com.sw.haruka.model.entity.FileShareTask;
import com.sw.haruka.ui.widget.ProgressView;

import java.util.List;

/**
 * Created by Swsbty on 2021/05/29
 */
public class MonitorAdapter extends BaseQuickAdapter<FileShareTask, BaseViewHolder> {
    public MonitorAdapter(List<FileShareTask> data) {
        super(R.layout.layout_download_card, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, FileShareTask task) {
        holder.setText(R.id.textView_fileName, task.getFileName());   // 文件名
        int position = getItemPosition(task);
        if (task.getFileName().contains(".")) { // 设置文件类型图片
            ImageView type = holder.getView(R.id.imageView_fileType);
            type.setImageDrawable(getContext().getDrawable(FileUtils.getTypeImage(FileUtils.getSuffix(task.getFileName()))));
        }
        holder.getView(R.id.imageButton_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FileRequestManager.getInstance().cancelTask(position);
                Toast.makeText(getContext(), "已取消任务", Toast.LENGTH_SHORT).show();
            }
        });
        if (task.getState() == FileShareTask.STATE_RUNNING) {   // 如果正在传输
            holder.setText(R.id.button_process, task.getProgress() + "%");// 进度，圆角按钮
            holder.setText(R.id.textView_speed, FileUtils.getFormatSize(task.getSpeed()) + "/s");// 传输速度
            holder.setText(R.id.textView_process, FileUtils.getFormatSize(task.getSharedSize())
                    + "/" + FileUtils.getFormatSize(task.getFileSize()));   // 已发大小
            ProgressView pv = holder.getView(R.id.progressBar_progress);   // 传输进度
//            pv.setProgressColor(getContext().getColor(R.color.progressColorA));
            pv.setPercentNow(task.getProgress());
        } else {        // 如果还没开始，就显示等待
            holder.setText(R.id.button_process, "等待");
            holder.setVisible(R.id.textView_speed, false);
            holder.setVisible(R.id.textView_process, false);
            holder.setVisible(R.id.progressBar_progress, false);
        }
    }
}
